#!/bin/bash
cd `dirname $0`
DIR=`pwd`
APP_NAME=code_gen
if [ "" != "$1" ];then
  APP_NAME=$1
fi

PIDS=`ps -ef | grep "${DIR}/${APP_NAME}" | grep -v grep | awk '{print $2}'`
if [[ -z "$PIDS" ]]; then
    echo "ERROR: The ${APP_NAME} does not started!"
    exit 1
fi

echo -e "Stopping the ${APP_NAME} ...\c"
for PID in $PIDS ; do
    kill $PID > /dev/null 2>&1
done

COUNT=0
while [ $COUNT -lt 1 ]; do
    echo -e ".\c"
    sleep 1
    COUNT=1
    for PID in $PIDS ; do
        PID_EXIST=`ps -f -p $PID | grep ${APP_NAME}`
        if [ -n "$PID_EXIST" ]; then
            COUNT=0
            break
        fi
    done
done

echo "OK!"
echo "PID: $PIDS"
